PHP just does some things better. cloud backups, pecl-expect
Published 2013-03-04 00:00:00
Backups, yes we do backup occasionally, and I've been looking for a better solutions than my historical office<->hosting replication, which although cheap, always made me wonder if I was still making copies. So after our recent office move, and minor server upgrade, I thought better double check on it all.. As usual, the thing had failed due to various reasons, and needed replacing.
So since I've been thinking about a new solution for our clients, I decided to go ahead and try it out for our data. I've been googling affordable cloud based storage for a while and found a company onlinestoragesolutions.com, the pricing is very reasonable US$45 for 2 years currently, with unlimited everything. There are a few review sites that seem to throw some cold water over the offer, but I had one client sign up without any major issues (for another reason), so at that price I though let's give it a go....
One of the key features they advertise is rsync, which since we run all linux machines would be ideal. However after paying, and getting access, I realized it's not quite a simple as pointing your rsync at their server. You need to set up an ssh tunnel to route rsync through.
Can't be that difficult I thought.... Turns out that setting up a password based ssh tunnel, automatically on a cron job, is no small task, there are questions all over stackexchange and various forums, none that I saw managed to find a solution. Most of the suggestions are based around the 'expect' program, a usefull unix tool which can be used to script ssh access. The problem in this case was that setting up the tunnel, then doing the rsync, and then closing the tunnel is not something that a bash, expect or any other method I found could do easily if at all.
So almost at the point of giving up, I started looking around at php's popen (which would not work either), and fell over the pecl expect extension.
Below is the result of a few minutes coding, which does exactly what is needed, and can be run directly from cron. Feel free to escape from overpriced backup solutions..
#!/usr/bin/php <?php $username = 'xxxx'; $password = 'xxxx'; $backup_what = '/home'; $target = 'myhome' ini_set("expect.loguser", "Off"); // forward remote port on the ols server:873 to localhost:9873 $stream = fopen("expect://ssh {$username}@fm.ols18.com -L9873:localhost:873", "r"); $cases = array ( array ("password:", 'password'), array( "yes/no)?", 'yes'), array( "Terminal", 'terminal'), ); while (true) { $x = expect_expectl ($stream, $cases); switch ($x) { case 'terminal': break; case 'password': fwrite ($stream, $password."\n"); break; case 'yes': fwrite ($stream, "yes\n"); break; default: echo $x; // usuall -1 as line was not matched.. die ("Error was occurred while connecting to the remote host!\n"); } if ($x == 'terminal') { break; } //echo "break while"; } putenv('RSYNC_PASSWORD='.$password); passthru("/usr/bin/rsync -a {$backup_what} rsync://{$username}@localhost:9873/{$username}/{$target}'); fclose ($stream);
Follow us
-
- Some thoughts on the language server and its usefulness in the roobuilder
- Roo Builder for Gtk4 moving forward
- Clustered Web Applications - Mysql and File replication
- GitLive - Branching - Merging
- PDO_DataObject Released
- PDO_DataObject is under way
- Mass email Marketing and anti-spam - some of the how-to..
- Hydra - Recruitment done right
Blog Latest
-
Twitter - @Roojs